package com.google.gwt.gfx.client.graphics.vml;

import java.util.List;

import com.google.gwt.gfx.client.graphics.Path;
import com.google.gwt.gfx.client.shared.DoubleControlPoint;
import com.google.gwt.gfx.client.shared.Point;
import com.google.gwt.user.client.DOM;

public class VMLPath extends Path {

	public VMLPath() {
		super(DOM.createElement("v:shape"));
	}

	private void appendPath(String tail) {
		DOM.setElementProperty(getElement(), "v",
				DOM.getElementProperty(getElement(), "v") + tail);
	}

	public void arcTo(int centerLeft, int centerTop, int width, int height,
			double start, double finish) {
		appendPath(" AE " + centerLeft + " " + centerTop + " " + width + " "
				+ height + " " + VML.fromAngle(start) + " "
				+ VML.fromAngle(finish));
	}

	@Override
	public void close() {
		appendPath(" X");
	}

	@Override
	public void curveTo(List<DoubleControlPoint> cpoints) {
		StringBuilder builder = new StringBuilder();
		for (DoubleControlPoint point : cpoints) {
			builder.append(point.getControlLeft1() + " "
					+ point.getControlTop1() + " " + point.getControlLeft2()
					+ " " + point.getControlTop2() + " " + point.getLeft()
					+ " " + point.getTop() + " ");
		}
		appendPath(" C " + builder.toString());
	}

	@Override
	public void lineTo(List<Point> points) {
		StringBuilder builder = new StringBuilder();
		for (Point point : points) {
			builder.append(point.getLeft() + " " + point.getTop() + " ");
		}
		appendPath(" L " + builder.toString());
	}

	@Override
	public void moveTo(int left, int top) {
		appendPath(" M " + left + " " + top);
	}

	@Override
	public void smoothQuadCurveTo(List<Point> points) {
		StringBuilder builder = new StringBuilder();
		for (Point point : points) {
			builder.append(point.getLeft() + " " + point.getTop() + " ");
		}
		appendPath(" QB " + builder.toString());
	}

}
